package by.amakarevich.webapp.page.registration;

import javax.inject.Inject;

import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.validation.EqualPasswordInputValidator;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;

import by.amakarevich.datamodel.ShoppingCart;
import by.amakarevich.datamodel.UserAccount;
import by.amakarevich.datamodel.enums.UserRole;
import by.amakarevich.services.ShoppingCartService;
import by.amakarevich.webapp.page.AlternativeBaseLayout;
import by.amakarevich.webapp.page.home.HomePage;

public class RegistrationPage extends AlternativeBaseLayout{
	@Inject
	private ShoppingCartService shoppingCartService;
	
	private String cpassword;
	
	public RegistrationPage() {
		super();
		final ShoppingCart shoppingCart=new ShoppingCart();
		final UserAccount userAccount=new UserAccount();
		
		

		final TextField<String> tfFirstName = new TextField<String>("firstName",new PropertyModel<String>(userAccount, "firstName"));
		tfFirstName.add(new PropertyValidator<String>());
		tfFirstName.setLabel(new ResourceModel("p.user.account.first.name"));
		

		final TextField<String> tfLastName = new TextField<String>("lastName",new PropertyModel<String>(userAccount, "lastName"));
		tfLastName.add(new PropertyValidator<String>());
		tfLastName.setLabel(new ResourceModel("p.user.account.last.name"));
		
		
		final TextField<String> tfEmail = new TextField<String>("email", new PropertyModel<String>(userAccount, "email"));
		tfEmail.add(new PropertyValidator<String>());
		tfEmail.setLabel(new ResourceModel("p.user.account.email"));
		
		
		final PasswordTextField tfPassword = new PasswordTextField("password", new PropertyModel<String>(userAccount, "password"));
		tfPassword.add(new PropertyValidator<String>());
		tfPassword.setLabel(new ResourceModel("p.user.account.password"));
		
		final PasswordTextField tfCPassword = new PasswordTextField("cpassword", new PropertyModel<String>(this,"cpassword"));
		tfCPassword.add(new PropertyValidator<String>());
		tfCPassword.setLabel(new ResourceModel("p.user.account.password"));
		
		Form<?> form = new Form<Void>("form");
		
		form.add(tfFirstName);
		form.add(tfLastName);
		form.add(tfEmail);
		form.add(tfPassword);
		form.add(tfCPassword);
		form.add(new EqualPasswordInputValidator(tfPassword, tfCPassword));
		
		form.add(new SubmitLink("sumbit-link") {
			@Override
			public void onSubmit() {
				super.onSubmit();
				userAccount.setStatus(true);
				userAccount.setRole(UserRole.user);
				userAccount.setInBlackList(false);
				
				shoppingCart.setTotalPrice(0l);
				
				shoppingCartService.createNewShoppingCart(shoppingCart, userAccount);
				
				setResponsePage(new HomePage());
			}

			@Override
			public void onError() {

				super.onError();
			}
		});

		add(form);

	}


}